package com.hp.gxw.api;

import com.hp.gxw.constant.R;
import com.hp.gxw.entity.Customer;
import com.hp.gxw.service.ICustomerService;
import com.hp.gxw.u8.EFWebService;
import com.hp.gxw.u8.U8ApiUtils;
import com.hp.gxw.u8.U8WebXMLResponseU8WebXMLResult;
import com.hp.gxw.u8.dto.CustomerParameter;
import com.hp.gxw.u8.dto.U8ApiResultDto;
import com.hp.gxw.u8.xml.CustomerXml;
import com.hp.gxw.utils.DateUtil;
import org.apache.axis.message.MessageElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

@Component
@RequestMapping("/api/customer")
public class CustomerApi {

    @Autowired
    private U8ApiUtils u8ApiUtils;

    @Autowired
    private ICustomerService iCustomerService;

	@Autowired
	private EFWebService eFWebServiceLocator;
	
	@Value("${u8client.serviceId}")
    private String serviceId;

	@Value("${u8client.environment}")
    private String environment;

    @RequestMapping("/register")
    @ResponseBody
    public R customer_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        CustomerParameter customerParameter = new CustomerParameter();
        Customer customer = new Customer();
        if(map.get("code") == null){
            return R.error("code为必填项");
        }
        customerParameter.setCode(map.get("code").toString());
        customer.setCode(map.get("code").toString());
        if(map.get("name") == null){
            return R.error("name为必填项");
        }
        customerParameter.setName(map.get("name").toString());
        customer.setName(map.get("name").toString());
        if(map.get("abbrname") == null){
            return R.error("abbrname为必填项");
        }
        customerParameter.setAbbrname(map.get("abbrname").toString());
        if(map.get("sort_code") == null){
            return R.error("sort_code为必填项");
        }
        customerParameter.setSort_code(map.get("sort_code").toString());
        if(map.get("domain_code") != null){
            customerParameter.setDomain_code(map.get("domain_code").toString());
        }
        if(map.get("tax_reg_code") != null){
            customerParameter.setTax_reg_code(map.get("tax_reg_code").toString());
        }
        if(map.get("ccusexch_name") != null){
            customerParameter.setCcusexch_name(map.get("ccusexch_name").toString());
        }else{
            customerParameter.setCcusexch_name("人民币");
        }
        if(map.get("bank_open") != null){
            customerParameter.setBank_open(map.get("bank_open").toString());
        }
        if(map.get("legal_man") != null){
            customerParameter.setLegal_man(map.get("legal_man").toString());
        }
        if(map.get("cRegCash") != null){
            customerParameter.setCRegCash(map.get("cRegCash").toString());
        }
        if(map.get("bank_acc_number") != null){
            customerParameter.setBank_acc_number(map.get("bank_acc_number").toString());
        }
        if(map.get("address") != null){
            customerParameter.setAddress(map.get("address").toString());
        }
        if(map.get("contact") != null){
            customerParameter.setContact(map.get("contact").toString());
        }
        if(map.get("email") != null){
            customerParameter.setEmail(map.get("email").toString());
        }
        if(map.get("phone") != null){
            customerParameter.setPhone(map.get("phone").toString());
        }
        if(map.get("mobile") != null){
            customerParameter.setMobile(map.get("mobile").toString());
        }
        if(map.get("bServiceAttribute") != null){
            customerParameter.setBServiceAttribute(map.get("bServiceAttribute").toString());
        }
        if(map.get("bCusDomestic") != null){
            customerParameter.setBCusDomestic(map.get("bCusDomestic").toString());
        }
        if(map.get("bCusOverseas") != null){
            customerParameter.setBCusOverseas(map.get("bCusOverseas").toString());
        }
        if(map.get("bServiceAttribute") == null && map.get("bCusDomestic") == null && map.get("bCusOverseas") == null){
            customerParameter.setBCusDomestic("1");
        }
        if(map.get("seed_date") != null){
            customerParameter.setSeed_date(DateUtil.getDateTimeFormat(map.get("seed_date").toString()));
        }else{
            customerParameter.setSeed_date(new Date());
        }
        if(map.get("dcuscreatedatetime") != null){
            customerParameter.setDcuscreatedatetime(DateUtil.getDateTimeFormat(map.get("dcuscreatedatetime").toString()));
        }else{
            customerParameter.setDcuscreatedatetime(new Date());
        }
        if(map.get("CreatePerson") != null){
//            return R.error("CreatePerson为必填项");
            customerParameter.setCreatePerson(map.get("CreatePerson").toString());
        }else{
            customerParameter.setCreatePerson("api");
        }
        customerParameter.setCcusmngtypecode("999");
        customerParameter.setEfserverid(serviceId);
        if(environment.equals("pro")){
            U8WebXMLResponseU8WebXMLResult u8WebXML = null;
            try {
                u8WebXML = eFWebServiceLocator.getEFWebServiceSoap().u8WebXML(CustomerXml.createXML(customerParameter));
                //解析返回结果
                MessageElement[] data = u8WebXML.get_any();
                System.out.println(data[0].getAttribute("succeed").toString());
                System.out.println(data[0].getAttribute("dsc").toString());
                if("0".equals(data[0].getAttribute("succeed").toString())) {
                    return R.error("接口异常"+data[0].getAttribute("dsc").toString());
                }
                customer.setCreateTime(new Date());
                iCustomerService.save(customer);
            }  catch (Exception e) {
                e.printStackTrace();
                return R.error("接口异常");
            }
        }else if(environment.equals("dev")){
            try {
                U8ApiResultDto resultDto = u8ApiUtils.httpRequest(CustomerXml.createXML(customerParameter).split("\n")[1]);
                if(resultDto == null || "0".equals(resultDto.getCode())) {
                    return R.error(resultDto==null?"接口异常":resultDto.getMsg());
                }
                customer.setCreateTime(new Date());
                iCustomerService.save(customer);
            }catch (Exception e){
                e.printStackTrace();
                return R.error("接口异常");
            }
        }
        return R.ok("写入成功");
    }
}
